<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="添加账户" :visible.sync="dialogVisible" @close="resetForm()" width="600px">
        <el-input placeholder="请输入账户ID、账户名称进行搜索" clearable prefix-icon="el-icon-search" v-model="querytext">
        </el-input>
        <div v-loading="loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0)">
            <el-table :data="filteredData(tableData,querytext)" class="com_table" height="400px" :row-key="(row)=>{ return row.advertiser_id}" style="margin-top:20px" ref="planRef" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" :reserve-selection="true">
                </el-table-column>
                <el-table-column prop="advertiser_id" label="账户ID" width="180">
                </el-table-column>
                <el-table-column prop="name" label="账户名称">
                </el-table-column>
            </el-table>
        </div>
        <!-- <el-pagination style="margin-top:20px" background layout="prev, pager, next" @current-change="handleCurrentChange" :total="tableData.length">
        </el-pagination> -->
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm()" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm()" class="el-button-width">添 加</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            pagesize: 10,
            currentPage: 1,
            type: null,
            hasCheck: [],
            planData: [],
            querytext: "",
            tableData: [],
        };
    },
    created() {
        this.$bus.on("addProUser", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.planData = value.planData;
            this.type = value.type;
            this.getDataList(value.planData, 1);
        });
    },
    beforeDestroy() {
        this.$bus.off("addProUser");
    },
    methods: {
        //获取广告组
        async getDataList(hasdata) {
            this.loading = true;
            let channel = this.$route.query.channel;
            if (channel == 3) {
                var { data: res } = await this.$http.post(
                    "adminapi/project/advertiserListAD"
                );
            } else {
                var { data: res } = await this.$http.post(
                    "adminapi/project/advertiserList"
                );
            }
            if (res.code !== 200) {
                this.loading = false;
                this.$message.error(res.msg);
                return;
            } else {
                this.loading = false;
                this.tableData = res.data;
                this.currentPage = 1;
                this.$nextTick(() => {
                    let arr = hasdata.map((item) => {
                        return item.advertiser_id;
                    });
                    this.tableData.forEach((row) => {
                        if (arr.indexOf(row.advertiser_id) >= 0) {
                            this.$refs.planRef.toggleRowSelection(row, true);
                        }
                    });
                });
            }
        },

        // 添加
        submitForm() {
            let arr = [];
            for (let i in this.hasCheck) {
                arr.push({
                    advertiser_id: this.hasCheck[i].advertiser_id,
                    name: this.hasCheck[i].name,
                });
            }
            this.$parent.addProjectForm.advertiser = arr;
            if (this.type == 1) {
                this.$parent.setSessionStorage();
            }
            this.resetForm();
        },

        // 关闭重置表单
        resetForm() {
            this.hasCheck = [];
            this.tableData = [];
            this.querytext = "";
            this.currentPage = 1;
            if (this.type == 1) {
                this.$refs.planRef.clearSelection();
            }
            this.dialogVisible = false;
        },

        // 筛选
        filteredData(list, query) {
            var list = list,
                query = query;

            if (!query) {
                return list;
            }
            query = query.trim();

            list = list.filter(function (item) {
                if (
                    item.name.indexOf(query) !== -1 ||
                    item.advertiser_id == query
                ) {
                    return item;
                }
            });
            return list;
        },

        // 表格选中
        handleSelectionChange(v) {
            this.hasCheck = v;
        },

        // 每页显示多少条数据
        handleSizeChange(val) {
            this.queryInfo.limit = val;
            this.getDataList(this.planData, 1);
        },
        // 当前页切换
        handleCurrentChange(val) {
            this.currentPage = val;
            // this.setpage = val;
            // this.getDataList(this.planData, val);
        },
    },
    components: {},
};
</script>


<style scoped>
</style>
